
#include "include.h"

volatile u32 u32TimBaseCnt =0u;

void Nvic_InPWM(void)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
}

void INPWM_TIM_Init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_ICInitTypeDef TIM_ICInitStruct;
	
	INPWM_RCCClockCmd(); 
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period = INPWM_TIM_ARR;
	TIM_TimeBaseInitStruct.TIM_Prescaler = INPWM_TIM_PSC;
	
	TIM_TimeBaseInit(INPWM_TIM, &TIM_TimeBaseInitStruct);
	
	TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
	TIM_ICInitStruct.TIM_ICFilter = 0;
	TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Falling;
	TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_PWMIConfig(INPWM_TIM,&TIM_ICInitStruct);
	
	TIM_SelectInputTrigger(INPWM_TIM, TIM_TS_TI1FP1);
	TIM_SelectSlaveMode(INPWM_TIM, TIM_SlaveMode_Reset);
	TIM_SelectMasterSlaveMode(INPWM_TIM, TIM_MasterSlaveMode_Enable);
	
	Nvic_InPWM();
	TIM_ITConfig(INPWM_TIM,TIM_IT_CC1,ENABLE);
	TIM_ClearITPendingBit(INPWM_TIM,TIM_IT_CC1);
	
	TIM_ARRPreloadConfig(INPWM_TIM,ENABLE);
	TIM_Cmd(INPWM_TIM,ENABLE);
	
}

void TIM6_IRQHandler(void)
{
	if(TIM_GetITStatus(TIMBase,TIM_IT_Update)==SET)
	{
		TIM_ClearITPendingBit(TIMBase,TIM_IT_Update);
		u32TimBaseCnt++;
	}else{;}
}

void Nvic_TIMBase(void)
{
	NVIC_InitTypeDef NVIC_InitStruct;
	
	NVIC_InitStruct.NVIC_IRQChannel = TIM6_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);
}

void TIMBase_Init(void)
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	
	TIMBase_RCCClockCmd();
	
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period = TIMBase_TIM_ARR;
	TIM_TimeBaseInitStruct.TIM_Prescaler = TIMBase_TIM_PSC;
	
	TIM_TimeBaseInit(TIMBase, &TIM_TimeBaseInitStruct);
	
	Nvic_TIMBase();
	TIM_ITConfig(TIMBase,TIM_IT_Update,ENABLE);
	TIM_ClearITPendingBit(TIMBase,TIM_IT_Update);
	
	TIM_ARRPreloadConfig(TIMBase,ENABLE);
	TIM_Cmd(TIMBase,ENABLE);
	
}








void Tim_Config(void)
{
//	INPWM_TIM_Init();
	TIMBase_Init();
}



